package com.example.demo.Controller;

import com.example.demo.Model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/test2")
public class TestController2 {
    // 传递单个参数
    @RequestMapping("/single")
    public String wordOfFamily(String word){
        return word;
    }

    //传递对象
    @RequestMapping("/user")
    public User userMessage(User user){
        return user;
    }

    // 后端参数重命名
    @RequestMapping("/name")
    public String name(@RequestParam(value = "time", required = false) String createtime ){
        return createtime;
    }

    // 接收 json 格式
    @RequestMapping("/user_json")
    public User userJson(@RequestBody User user){
        return user;
    }

    @RequestMapping("/html")
    public String html(){
        return "<h1>hello html<h1>";
    }

    // URL非参数部分获取参数
    @RequestMapping("/detail/{id}")
    public Integer detail(@PathVariable("id") Integer id){
        return id;
    }

    // URL 获取多个参数
    @RequestMapping("/detail2/{id}/{name}")
    public String detail2(@PathVariable Integer id, @PathVariable String name){
        return "id = "+id+" name = "+name;
    }

    // 上传文件
    @RequestMapping("upload")
    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
        String path = "C:\\image\\img.png";
        //保存文件
        file.transferTo(new File(path));
        return path;
    }

    // 上传文件修改版本
    @RequestMapping("upload1")
    public String upload1(@RequestPart("myfile")MultipartFile file) throws IOException {
        // 1. 生成一个唯一的 id | UUID
        String name = UUID.randomUUID().toString().replace("-","");
        // 2. 得到源文件的后缀名
        // 对完整文件名进行截取
        name += (file.getOriginalFilename().
                substring(file.getOriginalFilename().lastIndexOf(".")));
        String path = "C:\\image\\" + name ;
        //保存文件
        file.transferTo(new File(path));
        return path;
    }

    // 获取 Cookie
    @RequestMapping("/getcookie")
    public String cookie(@CookieValue(value = "java", required = false) String ck){
        return ck;
    }

    // 设置 Session
    @RequestMapping("/set_session")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session != null){
            session.setAttribute("SESSION_KEY", "栗子");
            return "session set successes";
        }else{
            return "session set fails";
        }
    }

    /**
     * 获取 Session
     */
    @RequestMapping("/get_session")
    public String getSession(@SessionAttribute(required = false, value = "SESSION_KEY") String name){
        return name;
    }

    // 获取 header
    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }



}
